<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户名" prop="uname">
        <el-input
          v-model="queryParams.uname"
          placeholder="请输入用户名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="现居住区" prop="nowarea">
        <el-input
          v-model="queryParams.nowarea"
          placeholder="请输入现居住区"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="健康码状态" prop="status" label-width="100px">
        <el-select v-model="queryParams.status" placeholder="请选择健康码状态" clearable>
          <el-option
            v-for="dict in dict.type.user_healthy_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

    <!--  <el-form-item label="注册时间" prop="regtime">
        <el-input
          v-model="queryParams.regtime"
          placeholder="请输入注册时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="微信号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入微信号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select v-model="queryParams.sex" placeholder="请选择性别" clearable>
          <el-option
            v-for="dict in dict.type.sys_user_sex"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="身份证号" prop="idnumber">
        <el-input
          v-model="queryParams.idnumber"
          placeholder="请输入身份证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="密码" prop="upwd">
        <el-input
          v-model="queryParams.upwd"
          placeholder="请输入密码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="r1" prop="r1">
        <el-input
          v-model="queryParams.r1"
          placeholder="请输入r1"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="r3" prop="r3">
        <el-input
          v-model="queryParams.r3"
          placeholder="请输入r3"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--<el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['frontuser:userman:add']"
        >新增</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['frontuser:userman:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['frontuser:userman:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['frontuser:userman:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="usermanList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户ID" align="center" prop="uid" />
      <el-table-column label="用户名" align="center" prop="uname" />
      <el-table-column label="注册时间" align="center" prop="regtime" width="115px"/>
      <el-table-column label="微信号" align="center" prop="phone" width="110px"/>
      <el-table-column label="性别" align="center" prop="sex" width="50px">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex"/>
        </template>
      </el-table-column>
      <el-table-column label="身份证号" align="center" prop="idnumber" width="170px"/>
      <el-table-column label="籍贯" align="center" prop="nativeplace" width="130px"/>
      <el-table-column label="现居住省" align="center" prop="nowprovince" />
      <el-table-column label="现居住市" align="center" prop="nowcity" />
      <el-table-column label="现居住区" align="center" prop="nowarea" />
      <el-table-column label="现居住街道" align="center" prop="nowaddress" />
      <el-table-column label="健康码状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.user_healthy_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="接种次数" align="center" prop="receivenum" />-->
<!--      <el-table-column label="身份证正面" align="center" prop="idfurl" />-->
<!--      <el-table-column label="身份证反面" align="center" prop="idburl" />-->
<!--      <el-table-column label="头像" align="center" prop="purl" />-->

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['frontuser:userman:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['frontuser:userman:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户名" prop="uname">
          <el-input v-model="form.uname" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="注册时间" prop="regtime">
          <el-input v-model="form.regtime" placeholder="请输入注册时间" />
        </el-form-item>
        <el-form-item label="微信号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入微信号" />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择性别">
            <el-option
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="身份证号" prop="idnumber">
          <el-input v-model="form.idnumber" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="密码" prop="upwd">
          <el-input v-model="form.upwd" placeholder="请输入密码" />
        </el-form-item>

        <el-divider content-position="center">usermanage信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <!--<el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddFrontslave">添加</el-button>
          </el-col>-->
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteFrontslave">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="frontslaveList" :row-class-name="rowFrontslaveIndex" @selection-change="handleFrontslaveSelectionChange" ref="frontslave">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="籍贯" prop="nativeplace" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.nativeplace" placeholder="请输入籍贯" />
            </template>
          </el-table-column>
          <el-table-column label="现居住省" prop="nowprovince" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.nowprovince" placeholder="请输入现居住省" />
            </template>
          </el-table-column>
          <el-table-column label="现居住市" prop="nowcity" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.nowcity" placeholder="请输入现居住市" />
            </template>
          </el-table-column>
          <el-table-column label="现居住区" prop="nowarea" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.nowarea" placeholder="请输入现居住区" />
            </template>
          </el-table-column>
          <el-table-column label="现居住街道" prop="nowaddress" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.nowaddress" placeholder="请输入现居住街道" />
            </template>
          </el-table-column>
          <el-table-column label="健康码状态" prop="status" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.status" placeholder="请选择健康码状态">
                <el-option
                  v-for="dict in dict.type.user_healthy_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="接种次数" prop="receivenum" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.receivenum" placeholder="请输入接种次数" />
            </template>
          </el-table-column>
          <el-table-column label="身份证正面地址uuid" prop="idfurl" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.idfurl" placeholder="请输入身份证正面地址uuid" />
            </template>
          </el-table-column>
          <el-table-column label="身份证反面地址uuid" prop="idburl" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.idburl" placeholder="请输入身份证反面地址uuid" />
            </template>
          </el-table-column>
          <el-table-column label="头像地址uuid" prop="purl" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.purl" placeholder="请输入头像地址uuid" />
            </template>
          </el-table-column>
          <el-table-column label="r1" prop="r1" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.r1" placeholder="请输入r1" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUserman, getUserman, delUserman, addUserman, updateUserman } from "@/api/frontuser/userman";

export default {
  name: "Userman",
  dicts: ['sys_user_sex', 'user_healthy_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedFrontslave: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户管理表格数据
      usermanList: [],
      // usermanage表格数据
      frontslaveList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        uname: null,
        // regtime: null,
        // phone: null,
        // sex: null,
        // idnumber: null,
        // upwd: null,
        // r1: null,
        // r3: null
        status: null,
        nowarea: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户管理列表 */
    getList() {
      this.loading = true;
      listUserman(this.queryParams).then(response => {
        // alert(JSON.stringify(response.rows))
        this.usermanList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        uid: null,
        uname: null,
        regtime: null,
        phone: null,
        sex: null,
        idnumber: null,
        upwd: null,
        r1: null,
        r3: null
      };
      this.frontslaveList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.uid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const uid = row.uid || this.ids
      getUserman(uid).then(response => {
        this.form = response.data;
        this.frontslaveList = response.data.frontslaveList;
        this.open = true;
        this.title = "修改用户管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.frontslaveList = this.frontslaveList;
          if (this.form.uid != null) {
            updateUserman(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUserman(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const uids = row.uid || this.ids;
      this.$modal.confirm('是否确认删除用户管理编号为"' + uids + '"的数据项？').then(function() {
        return delUserman(uids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** usermanage序号 */
    rowFrontslaveIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** usermanage添加按钮操作 */
    handleAddFrontslave() {
      let obj = {};
      obj.nativeplace = "";
      obj.nowprovince = "";
      obj.nowcity = "";
      obj.nowarea = "";
      obj.nowaddress = "";
      obj.status = "";
      obj.receivenum = "";
      obj.idfurl = "";
      obj.idburl = "";
      obj.purl = "";
      obj.r1 = "";
      this.frontslaveList.push(obj);
    },
    /** usermanage删除按钮操作 */
    handleDeleteFrontslave() {
      if (this.checkedFrontslave.length == 0) {
        this.$modal.msgError("请先选择要删除的usermanage数据");
      } else {
        const frontslaveList = this.frontslaveList;
        const checkedFrontslave = this.checkedFrontslave;
        this.frontslaveList = frontslaveList.filter(function(item) {
          return checkedFrontslave.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleFrontslaveSelectionChange(selection) {
      this.checkedFrontslave = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('frontuser/userman/export', {
        ...this.queryParams
      }, `userman_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
